<style type="text/css" media="screen">
    #custom_fields_list {
        width:415px;
    }
    div.custom_field {
        padding:5px;
        border:1px solid #466EB0;
        margin-bottom:1em;
    }
</style>
<script type="text/javascript" charset="utf-8">
    function remove_field(id)
    {
        $(id).remove();
    }
</script>
<h3>Custom Fields</h3>
<p>In addition to editable regions you specify in your template, you can use custom fields to either customize display options, add additional content area etc. <br>Fields created via the custom fields admin are shared between pages and news. 
<?php echo link_to('Create new custom fields here',
                    array('controller'=>'admin/custom_fields')) ?></p>
                    
<?php if($custom_fields) {?>
<div class='section'>
    <?php
    echo form_remote_tag(array('controller'=>'admin/custom_fields',
                          'action'=>'entries_custom_field'),
                    array('method'=>'get',
                          'update'=>'custom_fields_list',
                          'position'=>'Top'));
    ?>
    <?php echo hidden_field_tag('related_key','page_id') ?>
    <?php echo hidden_field_tag('related_value', $pages->get('id')) ?>
<b>Insert new custom field:</b> 
        <?php echo select_tag('id', options_for_select($custom_fields)); ?>
        <?php echo submit_tag('Insert') ?>


    <?php echo end_form_tag() ?>
</div>
<?php } ?>

<div id='custom_fields_list'>
    <?php
    $pages_custom_fields = $pages->custom_fields();
    if ($pages_custom_fields) {
        foreach ($pages_custom_fields as $field) 
        {
    ?>
    <?php $field_css_id = to_css_id('field_'.$field->get('id')); ?>
    <div id="custom_field_<?php echo $field_css_id ?>" class='custom_field'>
    <?php echo form_remote_tag(
                array('controller'=>'admin/custom_fields',
                      'action'=>'entries_custom_field'),
                array('update'=>'custom_field_'.$field_css_id)      
                  );
    ?>
    <?php echo hidden_field_tag('entries_custom_fields[id]',$field->get('id')); ?>
    <?php echo hidden_field_tag('entires_custom_fields[page_id]',$pages->get('id')) ?>
     <?php echo hidden_field_tag('entries_custom_fields[custom_field_id]', $field->get('custom_field_id')); ?>
    <?php echo hidden_field_tag('action','update',array('id'=>'action_'.$field_css_id)) ?>
    <h3><?php echo h($field->field_key()) ?></h3>
    <p><?php
    switch ($field->field_type()) {   
        case 'text':
            echo text_field_tag('entries_custom_fields[field_value]',
                               $field->get('field_value'),
                               array('size'=>'50',
                                     'id'=>$field_css_id));
            break;     
        case 'textarea':
            echo text_area_tag('entries_custom_fields[field_value]',
                               $field->get('field_value'),
                               array('size'=>'50x5',
                                     'id'=>$field_css_id));
            break;
        case 'date':
            $date = strtotime($field->get('field_date'));
            if (!$date || $date == '-1') {
                $date = time();
            }
            echo select_datetime(getdate($date), array('prefix'=>'entries_custom_fields[field_date]'));
            break;
    }
    ?></p>

    <p>
    <?php echo submit_tag('Update Field') ?>
    <?php echo submit_tag('Remove This Field?',
    array('onclick'=>"if(confirm('Are you sure you want to remove this field?')){ $('action_".$field_css_id."').value ='delete';remove_field('custom_field_{$field_css_id}')}",
          'class'=>'submit_delete')); ?></p>

    <?php echo end_form_tag() ?>
    </div>
    <?php                
        }
    }
    ?>
</div>